var Note = window.Note || {};
(function(){
	/**
	 *	Note Label Class
	 *	@namespace Note.Label
	 * 	@class Label
	 * 	@constructor
	 *	@param parentContainer {Object} 父容器
	 * 	@param oConfig {Object} 用户配置参数
	 */
	Note.Label = function(parentContainer, oConfig){
		this._parentContainer = parentContainer;
		this._initElements(oConfig.id);
		this._initConfig(oConfig);
		this._initEvents();
	};
	
	/**
	 *	Member Variable
	 */
	//数据
	Note.Label.prototype._id = null;
	
	//组件
	Note.Label.prototype._parentContainer = null;
	Note.Label.prototype._containerDiv = null;
	
	Note.Label.prototype._toggleDiv = null;
	Note.Label.prototype._toggleBtn = null;
	
	Note.Label.prototype._labelDiv = null;
	Note.Label.prototype._textDiv = null;
	Note.Label.prototype._deleteBtn = null;
	
	//回调对象
	Note.Label.prototype._isCallback = null;
	Note.Label.prototype._callbackObj = null;
	
	//组件的css类
	Note.Label.prototype._classNames = {
		containerDiv: "containerDiv",
		toggleDiv: "toggleDiv",
		toggleButton: "toggleButton",
		labelDiv: "labelDiv",
		textDiv: "textDiv",
		deleteButton: "deleteButton"
	};
	
	/**
	 *	函数名: _initElements
	 *	函数功能: 初始化label组件
	 *	@param id {String} label编号
	 */
	Note.Label.prototype._initElements = function(id){
		this._containerDiv = document.createElement("div");
		this._containerDiv.draggable = "true";
		this._containerDiv.id = "container" + id;
		this._containerDiv.className = this._classNames.containerDiv;
		
		this._toggleDiv = document.createElement("div");
		this._toggleDiv.id = "toggleDiv" + id;
		this._toggleDiv.className = this._classNames.toggleDiv;
		
		this._toggleBtn = document.createElement("div");
		this._toggleBtn.id = "toggleBtn" + id;
		this._toggleBtn.className = this._classNames.toggleButton;

		this._labelDiv = document.createElement("div");
		this._labelDiv.id = "label" + id;
		this._labelDiv.className = this._classNames.labelDiv;
		
		this._textDiv = document.createElement("div");
		this._textDiv.id = "text" + id;
		this._textDiv.className = this._classNames.textDiv;
		this._textDiv.contentEditable = true;
		
		this._deleteBtn = document.createElement("div");
		this._deleteBtn.id = "delete" + id;
		this._deleteBtn.className = this._classNames.deleteButton;
		
		this._toggleDiv.appendChild(this._toggleBtn);

		this._labelDiv.appendChild(this._textDiv);
		this._labelDiv.appendChild(this._deleteBtn);

		this._containerDiv.appendChild(this._toggleDiv);
		this._containerDiv.appendChild(this._labelDiv);
		
		this._parentContainer.appendChild(this._containerDiv);
	};
	
	/**
	 *	函数名: _initEvents
	 *	函数功能: 初始化Label标签框事件
	 */
	Note.Label.prototype._initEvents = function(){
		EventOnHandler(this._toggleBtn, "click", this, "toggleLabel");
		EventOnHandler(this._containerDiv, "dragend", this, "dragend");
		EventOnHandler(this._deleteBtn, "click", this, "deleteLabel");	
	};
	
	/**
	 *	函数名: _initConfig
	 *	函数功能: 初始化Label配置信息
	 *	@param oConfig {Object} label配置信息
	 */
	Note.Label.prototype._initConfig = function(oConfig){
		this._isCallback = oConfig.isCallback;
		if(oConfig.isCallback)
			this._callbackObj = oConfig.callbackObj;
		
		this._containerDiv.style.left = oConfig.left;
		this._containerDiv.style.top = oConfig.top;
		
		this.setId(oConfig.id);
		
		this.setText(oConfig.text);
		
		var display = "block";
		if(oConfig.display == false) display = "none";
		this._labelDiv.style.display = display;
	};
	
	/**
	 *	函数名: setId
	 *	函数功能: 设置Label ID
	 *	@param id {String} label编号
	 */
	Note.Label.prototype.setId = function(id){
		this._toggleBtn.innerHTML = id;
	};
	
	/**
	 *	函数名: setText
	 *	函数功能: 设置Label内容
	 *	@param Text {String} label的内容
	 */
	Note.Label.prototype.setText = function(Text){
		var contents = Text;
		if(contents == null) contents = "";
		this._textDiv.innerHTML = contents;
	};
	
	/**
	 *	函数名: save
	 *	函数功能: 保存当前Label数据
	 */
	Note.Label.prototype.save = function(){
		return {
			left: this._containerDiv.style.left,
			top: this._containerDiv.style.top,
			id: this._id,
			display: this._labelDiv.style.display == "block",
			text: this._textDiv.innerHTML,
			isCallback: this._isCallback,
			callbackObj: this._callbackObj
		};
	};
	
	/**
	 *	函数名: toggleLabel
	 *	函数功能: 切换label的显示或者隐藏状态
	 */
	Note.Label.prototype.toggleLabel = function(){
		if(this._labelDiv.style.display == "block"){	
	        this._labelDiv.style.display = "none";
		}
		else{
	        this._labelDiv.style.display = "block";
	    }
	};
	
	/**
	 *	函数名: deleteLabel
	 *	函数功能: 删除label
	 */
	Note.Label.prototype.deleteLabel = function(){
		this._parentContainer.removeChild(this._containerDiv);
		if(this._isCallback) this._callbackObj.deleteLabel(this);
	};
	
	/**
	 *	函数名: dragend
	 *	函数功能: 拖拽label
	 *	@param e {Object} 拖拽事件参数
	 */
	Note.Label.prototype.dragend = function(e){
		this._containerDiv.style.left = e.pageX + "px";
		this._containerDiv.style.top = e.pageY + "px";
	};
})();
